<?php

class Core_Table_Article_Category extends Kd_Data_Table {

    public function __construct() {
        parent::__construct();
    }

    public function getMaxSorting($parentId) {
        return Kd_Data_Factory::getQueryDcl()
                ->from($this->getName())
                ->where()->eq('parent_id', ':parentId:i')->endSelf()
                ->select()->field(Kd_Data_Dcl_Fn::max('sorting'), 'max_sorting')->closest()
                ->addParameter('parentId', $parentId)
                ->fetch()
                ->getFirstValue();
    }

    public function insertIdPath($id, $idPath, $transaction = NULL) {
        Kd_Data_Factory::getUpdateDcl()
                ->into($this->getName())
                ->value('id_path', ':idPath:s')
				->where()->eq('id', ':id:i')->endSelf()
                ->addParameter(array('id' => $id, 'idPath'=>$idPath))
                ->setDbDriver($this->getDbDriver($transaction))
                ->execute();
    }

    public function updateIdPath($oldIdPath, $newIdPath, $transaction = NULL){
    	Kd_Data_Factory::getUpdateDcl()
    			->into($this->getName())
    			->value('id_path', Kd_Data_Dcl_Fn::stringConcat("'$newIdPath'", Kd_Data_Dcl_Fn::stringSub('id_path', strlen($oldIdPath) + 1)))
    			->where()->like('id_path', "'$oldIdPath%'")
    			->closest()
    			->setDbDriver($this->getDbDriver($transaction))
    			->execute();
    }

	public function updateSorting($id, $position){
		Kd_Data_Factory::getUpdateDcl()
			->into($this->getName())
			->value('sorting', $position)
			->where()->eq('id', ':id:i')->closest()
			->addParameter('id', $id)
			->execute();
	}

}